package com.tulink.yichutu.controller.weixin;

import com.mxixm.fastboot.weixin.annotation.WxController;
import com.mxixm.fastboot.weixin.annotation.WxEventMapping;
import com.mxixm.fastboot.weixin.module.credential.WxJsTicketManager;
import com.mxixm.fastboot.weixin.module.event.WxEvent;
import com.mxixm.fastboot.weixin.module.media.WxMediaManager;
import com.mxixm.fastboot.weixin.module.message.WxMessageTemplate;
import com.mxixm.fastboot.weixin.module.user.WxUser;
import com.mxixm.fastboot.weixin.module.web.WxRequest;
import com.mxixm.fastboot.weixin.service.WxApiService;
import com.mxixm.fastboot.weixin.service.WxExtendService;
import com.mxixm.fastboot.weixin.web.WxUserManager;
import com.tulink.yichutu.biz.LoginBiz;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

/**
 * 公众号
 *
 * @author wanchaopeng
 * @date 2021/04/04 12:43
 */
@Slf4j
@WxController
public class GzhWxController {

    @Autowired
    private WxApiService wxApiService;
    @Autowired
    private WxMediaManager wxMediaManager;
    @Autowired
    private WxMessageTemplate wxMessageTemplate;
    @Autowired
    private  WxExtendService wxExtendService;
    @Autowired
    private WxJsTicketManager wxJsTicketManager;
    @Autowired
    private WxUserManager wxUserManager;
    @Autowired
    private LoginBiz loginBiz;

    /**
     * 接受微信事件 取消关注
     *
     * @param wxRequest
     * @param wxUser
     */
    @WxEventMapping(type = WxEvent.Type.UNSUBSCRIBE)
    public void unsubscribe(WxRequest wxRequest, WxUser wxUser) {
        log.info("取消关注：{},{}", wxRequest.getOpenId(), wxUser.getNickName());
        loginBiz.unsubscribe(wxUser);
    }

    /**
     * 接受微信事件 关注
     *
     * @param wxRequest
     * @param wxUser
     */
    @WxEventMapping(type = WxEvent.Type.SUBSCRIBE)
    public String subscribe(WxRequest wxRequest, WxUser wxUser) {
        log.info("关注公众号：{},{},{},{}", wxRequest.getOpenId(), wxRequest.getBody().getEventKey(), wxRequest.getBody().getScene(), wxUser.getNickName());
        String scene = wxRequest.getBody().getScene();
        if (!StringUtils.isEmpty(scene)) {
            return loginBiz.handleQrcodeScan(scene, wxUser);
        }
        log.info("非登录关注，{}", wxRequest.getOpenId());
        return null;
    }

    /**
     * 扫描带参二维码
     * @param wxRequest
     * @param wxUser
     * @return
     */
    @WxEventMapping(type = WxEvent.Type.SCAN)
    public String scan(WxRequest wxRequest, WxUser wxUser) {
        log.info("扫码：{},{},{},{}", wxRequest.getOpenId(), wxRequest.getBody().getEventKey(), wxRequest.getBody().getScene(), wxUser.getNickName());
        String scene = wxRequest.getBody().getScene();
        if (!StringUtils.isEmpty(scene)) {
            return loginBiz.handleQrcodeScan(scene, wxUser);
        }
        log.info("非登录扫码，{}", wxRequest.getOpenId());
        return null;
    }
}
